# add AD user manager full access to its M drive and mailbox
#Create the GUI Box
Function Read-MultiLineInputBoxDialog()
{
    Add-Type -AssemblyName System.Drawing
    Add-Type -AssemblyName System.Windows.Forms

# Create the Label.
    $label = New-Object System.Windows.Forms.Label
    $label.Location = New-Object System.Drawing.Size(10,10) 
    $label.Size = New-Object System.Drawing.Size(280,20)
    $label.AutoSize = $true
    $label.Text = "Please enter as below without blank line: 
LANID,Emailformanager(Y/N)
"
	
    # Create the TextBox used to capture the user's text.
    $Request = New-Object System.Windows.Forms.TextBox 
    $Request.Location = New-Object System.Drawing.Size(10,60) 
	$Request.Size = New-Object System.Drawing.Size(250,100)
    $Request.AcceptsReturn = $true
    $Request.AcceptsTab = $false
    $Request.Multiline = $true
    $Request.ScrollBars = 'Both'
    $Request.Text = "LANID,y"

    # Create the OK button.
    $OKButton= New-Object System.Windows.Forms.Button
    $OKButton.Location = New-Object System.Drawing.Size(30,180)
    $OKButton.Size = New-Object System.Drawing.Size(50,25)
    $OKButton.Text = "OK"
    $OKButton.Add_Click({ $temp= $Request.Text; $form.Close() })

	# Create the ClearText button.
    $ClearText = New-Object System.Windows.Forms.Button
    $ClearText.Location = New-Object System.Drawing.Size(100,180)
    $ClearText.Size = New-Object System.Drawing.Size(50,25)
    $ClearText.Text = "Clear"
    $ClearText.Add_Click({ $Request.Text = $null})

		# Create the Quit button.
    $QuitButton = New-Object System.Windows.Forms.Button
    $QuitButton.Location = New-Object System.Drawing.Size(180,180)
    $QuitButton.Size = New-Object System.Drawing.Size(50,25)
    $QuitButton.Text = "Quit"
    $QuitButton.Add_Click({ $temp="quit"; $form.Close() })
	
    # Create the form.
    $form = New-Object System.Windows.Forms.Form 
    $form.Text = "Disable User Management"
    $form.Size = New-Object System.Drawing.Size(300,250)
    $form.FormBorderStyle = 'FixedSingle'
    $form.StartPosition = "CenterScreen"
    $form.AutoSizeMode = 'GrowAndShrink'
    $form.Topmost = $True
  # $form.AcceptButton = $AddSendButton
  # $form.CancelButton = $AddReceiveButton
    $form.ShowInTaskbar = $true

    # Add all of the controls to the form.
    $form.Controls.Add($label)
	$form.Controls.Add($Request)
	$form.Controls.Add($ClearText)
    $form.Controls.Add($OKButton)
	$form.Controls.Add($QuitButton)
	
    # Initialize and show the form.
    $form.Add_Shown({$form.Activate()})
    $form.ShowDialog() > $null   # Trash the text of the button that was clicked.

    # Return the text that the user entered.
     return $temp
}
# Update the data that user enters
Function Trim-list($stringList)
{
	$temp=@()
	foreach($item in $stringList)
	{
		if($item.trim() -ne "" -and $item.trim() -ne $null ){
			$temp +=$item.trim()
		}
	}
	return $temp
}
$res = Read-MultiLineInputBoxDialog
if ($res -ne "quit")
{
	$erroractionpreference = "SilentlyContinue" 
	clear-content .\temp.txt
	clear-content .\NoExitID.txt
	$separator ="`r`n"
	$detail = $res  -split $separator
	$detail  = Trim-list($detail)
	if($detail.gettype().name -eq "String")	
	{
		write-host -foregroundcolor "yellow" "There is only 1 user to be processed"
	}else{
		$cnts = $detail.length
		write-host -foregroundcolor "yellow" "There are totally $cnts users to be processed"
	}
	$num =1

	foreach ($item in $detail)
	{
		if($item.trim() -eq "" -or $item.trim() -eq $null ){continue}
		wh -foregroundcolor "yellow" "--------Starting processing the $num user--------"
		$item = $item.split(';')
		$User=$item[0]
		$userExistStatus = gau $User
		if($userExistStatus -eq $null){
			echo "$User:NotExist" >>.\NoExitID.txt
			wh -foregroundcolor "yellow" "--------The $num user $User doesn't exist in AD system--------"
			$num=$num+1
			continue
		}
		$UserManager =  ((Get-ADUser $User -Properties Manager).Manager |Get-ADUser).SamAccountName
		$answerMail=$item.split(',')[1]
		$Path = (Get-ADUser $User -Properties HomeDirectory).HomeDirectory
	#<----------------------------Add User Manager access to M drive
		if ((test-path $Path) -eq $false){echo "The user $User doesn't have Home Drive" >>temp.txt}
		else{Add-NTFSAccess -Path $Path -Account EE\$UserManager -AccessRights FullControl}
	#<----------------------Add User Manager access to  email
		if($answerMail -eq "Y"){
			if(get-mailbox $User){
				$mail=(get-aduser $user -Properties mail).mail
				Add-MailboxPermission -Identity $mail -User EE\$UserManager -AccessRights 'FullAccess'
				set-Mailbox -Identity $user -GrantSendOnBehalfTo @{Add=$UserManager}
			}else{echo "The user $User doesn't have EA mailbox" >>temp.txt}
		}
	}
}
if ((cat .\NoExitID.txt).Length -gt 0){ii .\NoExitID.txt}
if ((cat .\temp.txt).Length -gt 0){ii .\temp.txt}